/*
 * @(#) RedisJobRecorder
 * 版权声明 厦门畅享信息技术有限公司, 版权所有 违者必究
 *
 * <br> Copyright:  Copyright (c) 2024
 * <br> Company:厦门畅享信息技术有限公司
 * <br> @author Administrator
 * <br> 2024-01-05 17:40:56
 */

package com.lost.octopus.es.processor.scheduler;

import com.lost.octopus.es.helper.service.DocumentEsService;
import com.lost.octopus.es.processor.constant.Constant;
import com.lost.octopus.es.processor.constant.enums.LimitSyncSchedulerKeyEnum;
import com.lost.octopus.es.processor.scheduler.entity.FullSyncInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * redis任务执行器
 * @author zhangbh
 * @date 2024/1/5 17:40
 */
@Component
@Log4j2
public class RedisJobRecorder {

    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private DocumentEsService documentEsService;


    /**
     * 获取正在同步的任务，任务名称等于索引名称
     * @param business 业务标识
     * @return 索引名称
     */
    public Optional<FullSyncInfo> runningJob(LimitSyncSchedulerKeyEnum business) {
        // 检查是否存在待继续任务
        String redisKey = redisKey(business);
        Boolean flag = redisTemplate.hasKey(redisKey);
        if (flag != null && flag) {
            String jobName = (String) redisTemplate.opsForHash().get(redisKey, "jobName");
            boolean indexExist = documentEsService.indexExist(jobName);
            if (!indexExist) {
                log.info("{} 全量调度索引不存在，可能人工操作了，直接当成无历史任务=====", business.getLabel());
                return Optional.empty();
            }
            Integer maxId = (Integer) redisTemplate.opsForHash().get(redisKey, "maxId");
            Integer dataCount = (Integer) redisTemplate.opsForHash().get(redisKey, "dataCount");
            return Optional.of(new FullSyncInfo(jobName, maxId, dataCount));
        }
        return Optional.empty();
    }

    /**
     * 更新全量进度
     * @param business 业务标识
     * @param jobName  任务名称
     */
    public void createProgress(LimitSyncSchedulerKeyEnum business, String jobName) {
        // 检查是否存在待继续任务
        String redisKey = redisKey(business);
        redisTemplate.opsForHash().put(redisKey, "jobName", jobName);
        redisTemplate.opsForHash().put(redisKey, "maxId", 0);
        redisTemplate.opsForHash().put(redisKey, "dataCount", 0);
    }

    /**
     * 更新全量进度
     * @param business  业务标识
     * @param maxId     当前跑的最大id
     * @param dataCount 数据量
     */
    public void updateProgress(LimitSyncSchedulerKeyEnum business, Integer maxId, Integer dataCount) {
        // 检查是否存在待继续任务
        String redisKey = redisKey(business);
        HashOperations opt = redisTemplate.opsForHash();
        opt.put(redisKey, "maxId", maxId);
        opt.put(redisKey, "dataCount", (Integer) opt.get(redisKey, "dataCount") + dataCount);
    }


    /**
     * 删除进度
     */
    public Boolean removeProgress(LimitSyncSchedulerKeyEnum key) {
        String redisKey = Constant.FULL_SYNC_REDIS_KEY_PREFIX + key.getValue();
        return redisTemplate.delete(redisKey);
    }

    /**
     * 获取redisKey
     * @param key 业务key
     * @return 累加前缀
     */
    protected String redisKey(LimitSyncSchedulerKeyEnum key) {
        return Constant.FULL_SYNC_REDIS_KEY_PREFIX + key.getValue();
    }
}
